home *** CD-ROM | disk | FTP | other *** search
- /*
- * File: DZInput.c
- *
- * Contents: Handles input devices.
- *
- * Copyright © 1996 Apple Computer, Inc.
- */
-
- #include <TextUtils.h>
-
- #include "InputSprocket.h"
-
- #include "DZGame.h"
- #include "DZInput.h"
- #include "DZResource.h"
-
- #define USE_MOUSE_AND_KEYBOARD 1
-
- enum {
- kDeviceList_COUNT = 100
- };
-
- enum {
- kElement_Fire,
- kElement_Pause,
- kElement_ShowHUD,
- kElement_ShowFPS,
- kElement_Turn,
- kElement_COUNT
- };
-
-
- static Boolean gInputActive = false;
- static ISpElementListReference gInputEventList = NULL;
- static ISpElementReference gInputElement[kElement_COUNT] = {NULL, NULL, NULL, NULL, NULL};
-
-
- /* =============================================================================
- * Input_Init (external)
- *
- * Initializes the Input stuff.
- * ========================================================================== */
- void Input_Init(
- void)
- {
- UInt32 count;
- ISpDeviceReference deviceList[kDeviceList_COUNT];
-
- ISpNeed needs[kElement_COUNT] =
- {
- { "\p", kIconSuiteID_Fire, 0, kISpElementKind_Button, kISpElementLabel_Fire, 0 },
- { "\p", kIconSuiteID_Pause, 0, kISpElementKind_Button, kISpElementLabel_Start, 0 },
- { "\p", kIconSuiteID_ShowHUD, 0, kISpElementKind_Button, kISpElementLabel_None, 0 },
- { "\p", kIconSuiteID_ShowFPS, 0, kISpElementKind_Button, kISpElementLabel_None, 0 },
- { "\p", kIconSuiteID_Turn, 0, kISpElementKind_Movement, kISpElementLabel_None, 0 },
- };
-
- // Get the names for the needs
- GetIndString(needs[kElement_Fire].name, kStrListID_NeedsNames, kElement_Fire+1);
- GetIndString(needs[kElement_Pause].name, kStrListID_NeedsNames, kElement_Pause+1);
- GetIndString(needs[kElement_ShowHUD].name, kStrListID_NeedsNames, kElement_ShowHUD+1);
- GetIndString(needs[kElement_ShowFPS].name, kStrListID_NeedsNames, kElement_ShowFPS+1);
- GetIndString(needs[kElement_Turn].name, kStrListID_NeedsNames, kElement_Turn+1);
-
- #if USE_MOUSE_AND_KEYBOARD
- // Enable the mouse
-
- // NOTE: This is not the correct way to handle the list count thing. We
- // should actually call once with NULL for the device list, malloc a list of
- // that size, and call again.
-
- ISpDevices_ExtractByClass(
- kISpDeviceClass_Mouse,
- kDeviceList_COUNT,
- &count,
- deviceList);
-
- if (count > kDeviceList_COUNT)
- {
- count = kDeviceList_COUNT;
- }
-
- ISpDevices_Activate(
- count,
- deviceList);
-
- // Enable the keyboard
- ISpDevices_ExtractByClass(
- kISpDeviceClass_Keyboard,
- kDeviceList_COUNT,
- &count,
- deviceList);
-
- if (count > kDeviceList_COUNT)
- {
- count = kDeviceList_COUNT;
- }
-
- ISpDevices_Activate(
- count,
- deviceList);
- #endif
-
- // Set our virtual elements
- ISpElement_NewVirtualFromNeeds(
- kElement_COUNT,
- needs,
- gInputElement,
- 0);
-
- // Autoconfigure our virtual elements based on our needs
- ISpInit(
- kElement_COUNT,
- needs,
- gInputElement,
- ' dz ',
- '????', // could use this as versioning on the needs
- 0,
- kSetListID,
- 0);
-
- // Build our list of elements, which we'll poll for events
- ISpElementList_New(
- 0,
- NULL,
- &gInputEventList,
- 0);
-
- // Add the virtual elements one at a time so we can assign a refcon
- ISpElementList_AddElements(
- gInputEventList,
- kInputEvent_Fire,
- 1,
- &gInputElement[kElement_Fire]);
-
- ISpElementList_AddElements(
- gInputEventList,
- kInputEvent_Pause,
- 1,
- &gInputElement[kElement_Pause]);
-
- ISpElementList_AddElements(
- gInputEventList,
- kInputEvent_ShowHUD,
- 1,
- &gInputElement[kElement_ShowHUD]);
-
- ISpElementList_AddElements(
- gInputEventList,
- kInputEvent_ShowFPS,
- 1,
- &gInputElement[kElement_ShowFPS]);
-
- // Start off suspended (because the game is stopped)
- ISpSuspend();
- }
-
-
- /* =============================================================================
- * Input_Exit (external)
- *
- * Prepares for exit.
- * ========================================================================== */
- void Input_Exit(
- void)
- {
- if (gInputActive)
- {
- Input_Activate(false);
- }
-
- if (gInputEventList != NULL)
- {
- ISpElementList_Dispose(gInputEventList);
- gInputEventList = NULL;
- }
-
- ISpStop();
-
- ISpElement_DisposeVirtual(kElement_COUNT, gInputElement);
- }
-
-
- /* =============================================================================
- * Input_Configure (external)
- *
- * Show the configuration dialog.
- * ========================================================================== */
- void Input_Configure(
- void)
- {
- ISpConfigure(NULL);
- }
-
-
- /* =============================================================================
- * Input_GetState (external)
- *
- * This routine handles the elements that are polled. It returns in outXTurn
- * and outYTurn the "turn" controls in the ±1 range.
- * ========================================================================== */
- void Input_GetState(
- float* outXTurn,
- float* outYTurn)
- {
- ISpMovementData movementData;
- float xTurn = 0.0;
- float yTurn = 0.0;
-
- if (gInputActive)
- {
- // Get the data
- ISpElement_GetComplexState(
- gInputElement[kElement_Turn],
- sizeof(ISpMovementData),
- &movementData);
-
- // Turn 'em into ±1 float range
- xTurn = movementData.xAxis;
- xTurn -= kISpAxisMiddle;
- xTurn /= kISpAxisMaximum-kISpAxisMiddle;
-
- yTurn = movementData.yAxis;
- yTurn -= kISpAxisMiddle;
- yTurn /= kISpAxisMaximum-kISpAxisMiddle;
- }
-
- // Return the values
- *outXTurn = xTurn;
- *outYTurn = yTurn;
- }
-
-
- /* =============================================================================
- * Input_GetEvent (external)
- *
- * This routine handles the elements that are event-based. It returns an
- * event code indicating which button has gone down. Note that we ignore
- * button-up transitions. Should be called repeatedly until it return
- * kInputEvent_None.
- * ========================================================================== */
- TInputEvent Input_GetEvent(
- void)
- {
- OSErr err;
- TInputEvent result = kInputEvent_None;
- ISpElementEvent event;
- Boolean gotEvent;
-
- if (gInputActive)
- {
- err = ISpElementList_GetNextEvent(gInputEventList, sizeof(event), &event, &gotEvent);
-
- if (err == noErr && gotEvent && event.data != 0)
- {
- result = (TInputEvent) event.refCon;
- }
- }
-
- return result;
- }
-
-
- /* =============================================================================
- * Input_Activate (external)
- *
- * On deactivation, we suspend InputSprocket. On activation we resume it and
- * flush the event queue. We only allow activation when the game is in "Play"
- * state.
- * ========================================================================== */
- void Input_Activate(
- Boolean inActivate)
- {
- Boolean doActivate;
-
- doActivate = inActivate;
- if (doActivate && Game_GetState() != kGameState_Playing)
- {
- doActivate = false;
- }
-
- if (gInputActive != doActivate)
- {
- gInputActive = doActivate;
-
- if (gInputActive)
- {
- #if USE_MOUSE_AND_KEYBOARD
- HideCursor();
- #endif
-
- ISpResume();
- ISpElementList_Flush(gInputEventList);
- }
- else
- {
- ISpSuspend();
-
- #if USE_MOUSE_AND_KEYBOARD
- ShowCursor();
- #endif
- }
- }
- }
-
-
-